package com.cwhuang.g7_tool_myhymns;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class PowerfulStatActivity extends Activity {
	public final static String COL_SERIAL = "col_serial";
	public final static String COL_INDEX = "col_index";
	public final static String COL_TITLE = "col_title";
	public final static String COL_RATING = "col_rating";
	public final static String COL_TIMES = "col_times";
	public final static String COL_FAMILIARITY = "col_familiarity";
	public static final int STAT_EDIT = 1;
	private Spinner spinnerStat;
	private ArrayAdapter<String> adapterSpinnerStat;
	private ListView listView;
	private HymnAdapterOneLine adapterListViewOneLine;
	AppHymns myHymns;
	Context context;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_powerful_stat);
        context = this;		//這行要在context.getApp 的前面，不然會讀不到myHymns
        myHymns = (AppHymns)getApplicationContext();
        
        getViews();
        setClickListeners();
        
    }

    private void setClickListeners() {
    	//點spinner的項目，可以依照所選項目條件排序
        spinnerStat.setOnItemSelectedListener( 
    		new OnItemSelectedListener() {
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					int choice = arg0.getSelectedItemPosition();
					
					//TODO 在編輯詩歌的那個地方，首先做排序，讓資料庫的順序回歸serial就很好處理
					
					//轉換成List用Collections的方法排序
					List list = myHymns.hymns;
					
					switch (choice) {
					case 0:
						//用serial
						Collections.sort(list, new PowerfulStatActivity.SerialCompare() );
						listView.setAdapter( new HymnAdapterOneLine
								(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
						break;
					case 1:
						//用index排
						//TODO "我編輯過的"可以把它拿掉
						Collections.sort(list, new PowerfulStatActivity.IndexCompare() );
						listView.setAdapter( new HymnAdapterOneLine
								(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
						break;
					case 2:
						//用rating排
				        Collections.sort(list, new PowerfulStatActivity.RatingCompare() );
				        listView.setAdapter( new HymnAdapterOneLine
								(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
						break;
					case 3:
						//用唱的次數排
						Collections.sort(list, new PowerfulStatActivity.TimesCompare() );
						listView.setAdapter( new HymnAdapterOneLine
								(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
						break;
					case 4:
						//用熟悉度排
						Collections.sort(list, new PowerfulStatActivity.FamiliarityCompare() );
						listView.setAdapter( new HymnAdapterOneLine
								(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
						break;
					default:
						break;
					}	//END switch
				}	//END onItemSelected()

				public void onNothingSelected(AdapterView<?> arg0) {}
		});//end statSpinner.setOnItemSelectedListener
        
      //點listView，能到DetailAdd那編輯
        listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				//要處理的資料其實只有title...因為其他都不是唯一的名稱= =
				LinearLayout linearLayout = (LinearLayout)arg1;
				TextView textViewIndex= (TextView)linearLayout.getChildAt(0);
				String index= textViewIndex.getText().toString();
				TextView textViewTitle = (TextView)linearLayout.getChildAt(1);
				String title = textViewTitle.getText().toString();
				
				//在這裡要把丟的資料處理好；在detailAdd.java要有bundle去接資料
				Intent intent = new Intent(context, DetailAddActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("col_index", index);
				bundle.putString("col_title", title);
				intent.putExtras(bundle);
				
				//到DetailAdd後回來，要把資料重新整理，所以用forResult
				startActivityForResult(intent, STAT_EDIT);
			}
		});	//end listview.setOnItemClickListener
        
	}	//END setClickListeners()
    
    //處理別的活動傳回來的東西。如果編輯了statActivity裡面的詩歌項目
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
   		//管你那麼多，照著spinner上所選的排序再更新→用愚蠢的方法更新
//    	listView.setAdapter( new HymnAdapterOneLine
//				(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
//    	spinnerStat.performItemClick(
//    			spinnerStat.getSelectedView(),
//    			spinnerStat.getSelectedItemPosition(),
//    			spinnerStat.getSelectedItemId() );
    	int choice = spinnerStat.getSelectedItemPosition();
    	
    	switch (choice) {
		case 0:
			//全部資料庫，修改後不用排序
			listView.setAdapter( new HymnAdapterOneLine
					(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
			break;
		case 1:
			//列出我編輯過的，修改後不用排
			listView.setAdapter( new HymnAdapterOneLine
					(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) ) );
			break;
		case 2:
			//最愛，要排
	    	spinnerStat.setSelection(0, false);	
	    	spinnerStat.setSelection(choice, false);
	    	break;
		case 3:
			//次數，要排
			spinnerStat.setSelection(0, false);	
	    	spinnerStat.setSelection(choice, false);
	    	break;
	    case 4:
	    	//熟悉度，要排
	    	spinnerStat.setSelection(0, false);	
	    	spinnerStat.setSelection(choice, false);
	    	break;
		default:
			break;
		}	//END switch
    	
    }	//END onActivityResult()
    
	private void getViews() {
		//把資料送給listview
		listView = (ListView)findViewById(R.id.listView_power_stat);
			//用turnHymnsToListOfMap()修飾raw data，成為呈現需要的資料
		adapterListViewOneLine= new HymnAdapterOneLine(context, myHymns.turnHymnsToListOfMap(myHymns.hymns) );   
        listView.setAdapter(adapterListViewOneLine); 
        
        //設定spinner
        spinnerStat = (Spinner)findViewById(R.id.spinner_power_stat);
        String[] spinnerStatItems = getResources().getStringArray(R.array.stat);
        adapterSpinnerStat = new ArrayAdapter<String>(
        		this,
        		android.R.layout.test_list_item, 
        		spinnerStatItems);
        	//setDropDownViewRecource可以避免setAdapter時，spinner變得又肥又醜
        adapterSpinnerStat.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerStat.setAdapter(adapterSpinnerStat);
        spinnerStat.setSelection(2);
	}

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_powerful_stat, menu);
        return true;
    }

	 //排序曲目編號
    static class SerialCompare implements Comparator<HashMap<String, Object>>{
    	public int compare(HashMap<String, Object> one, HashMap<String, Object> two){
    		int serial_one = Integer.parseInt( one.get(COL_SERIAL).toString() );
    		int serial_two = Integer.parseInt( two.get(COL_SERIAL).toString() );
    		return serial_one - serial_two;			//由低到高
    	}
    }
    
    //排序曲目編號
    static class IndexCompare implements Comparator<HashMap<String, Object>>{
    	public int compare(HashMap<String, Object> one, HashMap<String, Object> two){
    		int index_one = Integer.parseInt( one.get(COL_INDEX).toString() );
    		int index_two = Integer.parseInt( two.get(COL_INDEX).toString() );
    		return index_one - index_two;			//由低到高
    	}
    }
    
    //用來排序評分
    static class RatingCompare implements Comparator<HashMap<String, Object>>{
		public int compare(HashMap<String, Object> one, HashMap<String, Object> two){
			int rating_one = Integer.parseInt( one.get(COL_RATING).toString() );
			int rating_two = Integer.parseInt( two.get(COL_RATING).toString() );
			return rating_two - rating_one;			//由高到低
		}
	}
    
    //用來排序唱的次數
    static class TimesCompare implements Comparator<HashMap<String, Object>>{
    	public int compare(HashMap<String, Object> one, HashMap<String, Object> two){
    		int times_one = Integer.parseInt( one.get(COL_TIMES).toString() );
    		int times_two = Integer.parseInt( two.get(COL_TIMES).toString() );
    		return times_two - times_one;			//由高到低
    	}
    }
    
    //用來排序唱的次數
    static class FamiliarityCompare implements Comparator<HashMap<String, Object>>{
    	public int compare(HashMap<String, Object> one, HashMap<String, Object> two){
    		String familiarity_one = one.get(COL_FAMILIARITY).toString() ;
    		String familiarity_two = two.get(COL_FAMILIARITY).toString() ;
    		int value_one = getValue(familiarity_one);
    		int value_two = getValue(familiarity_two);
    			
    		return value_two - value_one;			//由高到低
    	}
    	private int getValue(String familiarityString){
    		int value=0;
    		if (familiarityString.equals("極熟") ){value = 5;}
    		else if (familiarityString.equals("極不熟") ){value = 4;}
    		else if (familiarityString.equals("很熟") ){value = 3;}
    		else if (familiarityString.equals("不熟") ) {value = 2;}
    		else if (familiarityString.equals("普通熟") ) {value = 1;}
    		return value;
    	}
    }
}
